package com.jt.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectMapperUtil {
    private static final ObjectMapper MAPPER=new ObjectMapper();
    public static String toJSON(Object obj){
        try {
            return MAPPER.writeValueAsString(obj);
        }catch (JsonProcessingException e){
//            将检查异常，转换为运行异常，转换为全局异常处理
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static <T> T toObj(String json,Class<T> target)  {
        try {
            return MAPPER.readValue(json,target);
        }catch (JsonProcessingException e){
            e.printStackTrace();
            throw new RuntimeException(e);

        }
    }

}
